﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using System.IO;

using VNode = System.Windows.Forms.TreeNode;

namespace Yuyi.PluginForJS.Addin
{
    public partial class NavigationTreeView : UserControl
    {
        private DTE m_dte = null;
        public DTE DTE
        {
            get { return m_dte; }
        }
        public NavigationTreeView()
        {
            InitializeComponent();
        }
        public void Init(DTE dte)
        {
            m_dte = dte;
        }
        public void LoadData()
        {
            //treeViewMembers.Nodes.Clear();
            //Document doc = DTE.ActiveDocument;
            //if (doc == null || !doc.FullName.EndsWith("js")) return;
            //string fileName = doc.FullName;
            //using (Lexer lexer = new Lexer(new StreamReader(fileName)))
            //{
            //    try
            //    {
            //        Parser parser = new Parser(lexer);
            //        TNode t = parser.Parse();
            //        if (t == null) return;
            //        foreach (TNode child in t.ChildNodes)
            //        {
            //            if (child != null && child.NodeKind == Nodes.functionDeclaration)
            //            {
            //                VNode node = new VNode();
            //                string param = null;
            //                IList<TNode> parameterNodes = child.ChildNodes[0].ChildNodes;
            //                if (parameterNodes.Count > 0)
            //                {
            //                    StringBuilder parameters = new StringBuilder();
            //                    foreach (TNode n in parameterNodes)
            //                    {
            //                        parameters.AppendFormat("{0},", n.Name);
            //                    }
            //                    param = parameters.ToString(0, parameters.Length - 1);
            //                }
            //                StringBuilder sb = new StringBuilder();
            //                sb.AppendFormat("{0}({1})", child.Name, (param == null) ? "" : param);
            //                node.Text = sb.ToString();
            //                node.Tag = child.Line.ToString();
            //                node.ImageKey = "function";
            //                treeViewMembers.Nodes.Add(node);
            //            }
            //        }
            //    }
            //    finally
            //    {
            //        lexer.Dispose();
            //    }
            //}
        }

        private void treeViewMembers_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (DTE.ActiveDocument == null || !DTE.ActiveDocument.FullName.EndsWith("js")) return;
            TextDocument doc = (TextDocument)DTE.ActiveDocument.Object("TextDocument");
            int line = Convert.ToInt32(e.Node.Tag.ToString());
            doc.Selection.GotoLine(line+1,true);

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

    }
}
